/**
 * Created by cc on 2016/11/17.
 */
$('.btn-login').on('click', function() {
	login_validate();
});
//新增键盘事件
$(document).on('keydown', function(e) {
	if(e && e.keyCode == 13) {
		login_validate();
	}
});

//登录验证
function login_validate() {
	var guid = newGuid();
	var para = {
		userName: $('.user_name').val(),
		userPassword: $('.user_pwd').val()
	}
	//增加输入校验
	if(para.userName != "" && para.userPassword != "") {
		$.ajax({
			url: url+'/login/login',
			type: "post",
			data: para,
			success: function(d) {
				console.log(d);
				if(d.code == 200) {
					$('.login_failed').css('display', 'none');
					///    本地缓存
					var str = JSON.stringify(d.content);
					var storage = window.sessionStorage;
					if(storage) {
						storage.setItem("user_msg", str);
                        storage.setItem("changetime", d.changePassword);
					} else {
						Cookie.write("user_msg", str);
						Cookie.write("changetime", d.changePassword);
					}
					if(d.content.userrole == 3) {
						self.location = '../../service/index.html';
					} else {
						self.location = "../../pagehome/index.html";
					};
				} else if(d.code == 204) {
					$('.login_failed').text("账号已被禁用,请联系管理员！");
					$('.login_failed').css('display', 'block');
				} else {

					$('.login_failed').text("账号或密码错误！");
					$('.login_failed').css('display', 'block');
				}

			}
		});

	} else {
		$('.login_failed').text("请输入账号或密码！");
		$('.login_failed').css('display', 'block');
	}

};

function newGuid() {
	var guid = "";
	for(var i = 1; i <= 32; i++) {
		var n = Math.floor(Math.random() * 16.0).toString(16);
		guid += n;
		if((i == 8) || (i == 12) || (i == 16) || (i == 20))
			guid += "-";
	}
	return guid;
}